package com.example.demo3.service.purplan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo3.entity.purplan.PurPlan;
import com.example.demo3.mapper.ResourceDeviceMapper;
import com.example.demo3.mapper.purplan.PurPlanMapper;
import com.example.demo3.utils.excelprocess.POIHelper;
import com.example.demo3.utils.strprocess.ObjectParse;
import com.example.demo3.utils.strprocess.StrUtil;
import com.example.demo3.utils.strprocess.Time_convert;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 采购计划表 服务类
 * </p>
 *
 * @author cjw
 * @since 2020-03-23
 */
@Service
@Transactional
public class PurPlanService extends ServiceImpl<PurPlanMapper, PurPlan> {
	
	@Resource
	private PurPlanMapper purPlanMapper;
	
	
	
	/**
	 * 
	 * @param
	 * @return 
	 * @author cjw 2020-03-24
	 */
	public Page<PurPlan> getPurPlanByPage(int pageNum, int pageSize,String planNo,String createTimeStartStr,String createTimeEndStr,int status){
		Page<PurPlan> page=new Page<PurPlan>().setCurrent(pageNum).setSize(pageSize);
		QueryWrapper<PurPlan> queryWrapper=new QueryWrapper<PurPlan>()
				.like(!StrUtil.isEmpty(planNo),"PLAN_NO",planNo)
				.ge(!StrUtil.isEmpty(createTimeStartStr),"TO_CHAR(CREATE_TIME,'yyyy-mm-dd hh24:mi:ss')",createTimeStartStr)
				.le(!StrUtil.isEmpty(createTimeEndStr),"TO_CHAR(CREATE_TIME,'yyyy-mm-dd hh24:mi:ss')",createTimeEndStr)
				.eq(status!=2,"STATUS", status)
				.eq("IS_DEL",0)
				.orderByDesc("CREATE_TIME");
		purPlanMapper.selectPage(page,queryWrapper);
		return page;
	}
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-24
	 */
	public List<PurPlan> getPurPlanList(String planNo,String createTimeStartStr,String createTimeEndStr,int status){
		QueryWrapper<PurPlan> queryWrapper=new QueryWrapper<PurPlan>()
				.like(!StrUtil.isEmpty(planNo),"PLAN_NO",planNo)
				.ge(!StrUtil.isEmpty(createTimeStartStr),"TO_CHAR(CREATE_TIME,'yyyy-mm-dd hh24:mi:ss')",createTimeStartStr)
				.le(!StrUtil.isEmpty(createTimeEndStr),"TO_CHAR(CREATE_TIME,'yyyy-mm-dd hh24:mi:ss')",createTimeEndStr)
				.eq(status!=2,"STATUS", status)
				.eq("IS_DEL",0)
				.orderByDesc("CREATE_TIME");
		List<PurPlan> list_purPlan= purPlanMapper.selectList(queryWrapper);
		return list_purPlan;
	}
	
	/**
	 * 
	 * @param 
	 * @return 
	 * @author cjw 2020-03-24
	 */
	public List<PurPlan> getPurPlanList(){
		
		QueryWrapper<PurPlan> queryWrapper_purPlan=new QueryWrapper<PurPlan>()
				.eq("IS_DEL",0)
				.orderByDesc("CREATE_TIME");
		List<PurPlan> list_purPlan=purPlanMapper.selectList(queryWrapper_purPlan);
		return list_purPlan;
	}
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-25
	 */
	public PurPlan getPurPlanById(String purPlanId){
		PurPlan purPlan=purPlanMapper.selectById(purPlanId);
		return purPlan;
	}
	
	/**
	 * 导出采购计划信息》将list_purPlan存入POIHelper中。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-26
	 */
	public POIHelper exportExecl(List<PurPlan> list_purPlan) {
		POIHelper poiHelper = new POIHelper();
		HSSFFont contentFont = poiHelper.definedFont(false, (short) 12, "宋体");
		HSSFCellStyle contentStyle = poiHelper.definedCellStyle(Font.COLOR_NORMAL, HorizontalAlignment.CENTER, true, contentFont);
		// 设置工作簿名称
		String sheetName = "Sheet1";
		HSSFSheet sheet = poiHelper.getHSSFSheet(sheetName);
		sheet.getPrintSetup().setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
		sheet.getPrintSetup().setLandscape(true);
		//存入表头和查询结果
		int rowIndex = poiHelper.exportHeadCommon(sheet, PurPlan.COLUMN_HEAD, PurPlan.COLUMN_WIDTH);
		HSSFRow row = null;
		int cellIndex = 0;
		
		for (PurPlan purPlan : list_purPlan) {
			rowIndex++;
			row = sheet.createRow(rowIndex);
			cellIndex = 0;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, purPlan.getPlanNo());
			cellIndex++;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, purPlan.getTitle());
			cellIndex++;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, purPlan.getPurType());
			cellIndex++;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, ObjectParse.parse_toStr(purPlan.getPlanPurNum()));
			cellIndex++;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, ObjectParse.parse_toStr(purPlan.getPlanAmount()));
			cellIndex++;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, Time_convert.convert_tostring(purPlan.getCreateTime()));
			cellIndex++;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, purPlan.getCreateUser());
			cellIndex++;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, purPlan.getDes());
			
			cellIndex++;
			//statusMeaning
			String statusMeaning = null;
			if (purPlan.getStatus() == null) {
				statusMeaning = null;
			} else if (purPlan.getStatus() == 0) {
				statusMeaning = "进行中";
			} else if (purPlan.getStatus() == 1) {
				statusMeaning = "已结束";
			}
			poiHelper.addXlsCell(contentStyle, row, cellIndex, statusMeaning);
			cellIndex++;
			poiHelper.addXlsCell(contentStyle, row, cellIndex, Time_convert.convert_tostring(purPlan.getFlowEndTime()));
		}
		return poiHelper;
	}
	
	
	
}
